package com.awspaas.user.apps.shhtaerospaceindustrial.extbutton;


import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ValueListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.service.util.ModelUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class updatePsnDataFromSastList2 extends ValueListener {

	@Override
	public String execute(ProcessExecutionContext param) throws Exception {
		// TODO Auto-generated method stub
		
		String bindid = param.getProcessInstance().getId();

		System.out.println("bindid::" + bindid);
		String querySql = "select ID,UNITNAME,PSNNAME,SPRETURNDATE,APPBACKUNITDATE,BELONGAREA,FGGLZHENGCHE,PSNLSNO from "
				+ "BO_EU_SHHT_CS_FGRYZZ_MX where " + "bindid='" + bindid + "' and  PSNLSNO is not null";
//		System.out.println("SQL����ǣ�" + querySql);
		List<Map<String, Object>> dataList = DBSql.query(querySql, new ColumnMapRowMapper());
		if (dataList != null && !dataList.isEmpty()) {
			for (Map<String, Object> dataMap : dataList) {

				String ID = objToStr(dataMap.get("ID"));
				String PSNLSNO = objToStr(dataMap.get("PSNLSNO"));
				String UNITNAME = objToStr(dataMap.get("UNITNAME"));
				String PSNNAME = objToStr(dataMap.get("PSNNAME"));

				if (PSNLSNO != null && !("").equals(PSNLSNO)) {
					String queryItemSql = "select UNITNAME,PSNNAME,SPRETURNDATE,APPBACKUNITDATE,BELONGAREA,FGGLZHENGCHE,PSNLSNO,ID from BO_EU_SHHT_SPFGRY_MX where  PSNLSNO='"
							+ PSNLSNO + "' and UNITNAME='" + UNITNAME + "' and PSNNAME='" + PSNNAME + "'";

					List<Map<String, Object>> itemdataList = DBSql.query(queryItemSql, new ColumnMapRowMapper());

					if (itemdataList != null && !itemdataList.isEmpty()) {
						for (Map<String, Object> itemdataMap : itemdataList) {
							String SPRETURNDATE = objToStr(itemdataMap.get("SPRETURNDATE"));
							 SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM-dd");   						
						        Date date = null;
						        try {
						            date = sdfm.parse(SPRETURNDATE);
						        } catch (ParseException e) {
						            e.printStackTrace();
						        }
						        SPRETURNDATE=sdfm.format(date);
							String BELONGAREA = objToStr(itemdataMap.get("BELONGAREA"));
							String FGGLZHENGCHE = objToStr(itemdataMap.get("FGGLZHENGCHE"));
							
							BO recordbo=SDK.getBOAPI().get("BO_EU_SHHT_CS_FGRYZZ_MX", ID);
							Map<String, Object> record = recordbo.asMap();
							
							record.put("SPRETURNDATE", SPRETURNDATE);
							record.put("BELONGAREA", BELONGAREA);
							record.put("FGGLZHENGCHE", FGGLZHENGCHE);
							
							int updateflag =SDK.getBOAPI().update("BO_EU_SHHT_CS_FGRYZZ_MX", ModelUtil.toModel(record));
							
//							if ((BELONGAREA != null && !("").equals(BELONGAREA))
//									&& (FGGLZHENGCHE != null && !("").equals(FGGLZHENGCHE))) {
//								String updatesql="UPDATE BO_EU_SHHT_CS_FGRYZZ_MX "
//										+ "SET "
//										+ "SPRETURNDATE = to_date('"+ SPRETURNDATE + "','yyyy-MM-dd'),"
//										+ " BELONGAREA='"+ BELONGAREA + "',"
//										+ "FGGLZHENGCHE='"+ FGGLZHENGCHE + "'"
//										+ " WHERE ID = '" + ID + "'";
////								
//								int updateflag = DBSql.update(updatesql);
								System.out.println("更新操作的结果是：" + updateflag);
//							}
						}
					}

				}

			}
		} else {
			return "未查询到需要更新的记录";
		}
		return "操作成功";
	}

	public static String objToStr(Object obj) {
		return obj == null ? "" : obj.toString();
	}

}
